package com.noticestation.presenter;

import java.util.ArrayList;

import org.apache.log4j.Logger;
import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceAware;

import winterwell.jtwitter.Twitter;

import com.noticestation.domain.BasicEvent;

/**
 * Class receives list of events and tweets them
 * all of the heavy lifting is done by jtwitter, http://www.winterwell.com/software/jtwitter.php
 * 
 * @author Matthew Phillips
 */
public class TwitterPresenter implements PresenterInterface, MessageSourceAware
{
	static Logger log = Logger.getLogger("TwitterPresenter");
	private MessageSource messageSource;

	/**
	 * Receives list of events and sends those events to twitter
	 * 
	 * @param events list of events to tweet
	 */
	public void present(ArrayList events)
	{
		String twitterUser = messageSource.getMessage("twitterUser", null, null);
		String twitterPass = messageSource.getMessage("twitterPass", null, null);
		
		for (Object event : events)
		{
			BasicEvent basicEvent = (BasicEvent)event;
			Twitter twitter = new Twitter(twitterUser,twitterPass);
			twitter.setStatus(basicEvent.getName() + ", tomorrow, " + basicEvent.getDate());
			log.info("Tweeting: " + basicEvent.getName() + ", tomorrow, " + basicEvent.getDate());
		}		
	}
	
	/**
	 * Spring's message source aware allows for sharing of messages (most
	 * likely used like a properties file)
	 * 
	 * @see org.springframework.context.MessageSourceAware#setMessageSource(org.springframework.context.MessageSource)
	 */
	public void setMessageSource(MessageSource messageSourece)
	{
		this.messageSource = messageSourece;		
	}

}
